home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / UUPC11QS.ARJ / GETOPT.C < prev    next >
C/C++ Source or Header  |  1991-04-22  |  3KB  |  86 lines

  1. /*
  2. ** @(#)getopt.c   2.2 (smail) 1/26/87
  3.  
  4.    01 Oct 89   Added function prototype for getopt()              ahd
  5. */
  6.  
  7. /*
  8.  * Here's something you've all been waiting for:  the AT&T public domain
  9.  * source for getopt(3).  It is the code which was given out at the 1985
  10.  * UNIFORUM conference in Dallas.  I obtained it by electronic mail
  11.  * directly from AT&T.  The people there assure me that it is indeed
  12.  * in the public domain.
  13.  *
  14.  * There is no manual page.  That is because the one they gave out at
  15.  * UNIFORUM was slightly different from the current System V Release 2
  16.  * manual page.  The difference apparently involved a note about the
  17.  * famous rules 5 and 6, recommending using white space between an option
  18.  * and its first argument, and not grouping options that have arguments.
  19.  * Getopt itself is currently lenient about both of these things White
  20.  * space is allowed, but not mandatory, and the last option in a group can
  21.  * have an argument.  That particular version of the man page evidently
  22.  * has no official existence, and my source at AT&T did not send a copy.
  23.  * The current SVR2 man page reflects the actual behavor of this getopt.
  24.  * However, I am not about to post a copy of anything licensed by AT&T.
  25.  */
  26.  
  27. #ifdef BSD
  28. #include <strings.h>
  29. #else
  30. #include <string.h>
  31. #define  index strchr
  32. #endif
  33. #include <stdio.h>
  34.  
  35. #define ERR(s,c) fprintf(stderr,"%s%s%c\n", argv[0],s,c)    /* ahd   */
  36.  
  37. #include "getopt.h"
  38.  
  39. int   opterr = 1;
  40. int   optind = 1;
  41. int   optopt;
  42. char  *optarg;
  43.  
  44. int getopt(int argc, char **argv, char *opts)
  45. {
  46.    static int sp = 1;
  47.    register int c;
  48.    register char *cp;
  49.  
  50.    if(sp == 1)
  51.       if(optind >= argc ||
  52.          argv[optind][0] != '-' || argv[optind][1] == '\0')
  53.          return(EOF);
  54.       else if(strcmp(argv[optind], "--") == 0) {
  55.          optind++;
  56.          return(EOF);
  57.       }
  58.    optopt = c = argv[optind][sp];
  59.    if(c == ':' || (cp=index(opts, c)) == NULL) {
  60.       ERR(": illegal option -- ", c);
  61.       if(argv[optind][++sp] == '\0') {
  62.          optind++;
  63.          sp = 1;
  64.       }
  65.       return('?');
  66.    }
  67.    if(*++cp == ':') {
  68.       if(argv[optind][sp+1] != '\0')
  69.          optarg = &argv[optind++][sp+1];
  70.       else if(++optind >= argc) {
  71.          ERR(": option requires an argument -- ", c);
  72.          sp = 1;
  73.          return('?');
  74.       } else
  75.          optarg = argv[optind++];
  76.       sp = 1;
  77.    } else {
  78.       if(argv[optind][++sp] == '\0') {
  79.          sp = 1;
  80.          optind++;
  81.       }
  82.       optarg = NULL;
  83.    }
  84.    return(c);
  85. }
  86.